﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using XYAuto.Utils.Data;

namespace XYAuto.MonitorData.Chitunion.Dal
{

    //监听任务表
    public partial class MonitorTaskInfo : DataBase
    {


        public static readonly MonitorTaskInfo Instance = new MonitorTaskInfo();


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Insert(Entities.MonitorTaskInfo entity)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into MonitorTaskInfo(");
            strSql.Append("MonitorName,MonitorSql,AchieveCount,DataServceId,NoticeKey,NoticeToValue,NoticeDesc,IsActive,CreateTime,CreateUserId,UpdateTime,UpdateUserId");
            strSql.Append(") values (");
            strSql.Append("@MonitorName,@MonitorSql,@AchieveCount,@DataServceId,@NoticeKey,@NoticeToValue,@NoticeDesc,@IsActive,@CreateTime,@CreateUserId,@UpdateTime,@UpdateUserId");
            strSql.Append(") ");
            strSql.Append(";select SCOPE_IDENTITY()");
            var parameters = new SqlParameter[]{
                        new SqlParameter("@MonitorName",entity.MonitorName),
                        new SqlParameter("@MonitorSql",entity.MonitorSql),
                        new SqlParameter("@AchieveCount",entity.AchieveCount),
                        new SqlParameter("@DataServceId",entity.DataServceId),
                        new SqlParameter("@NoticeKey",entity.NoticeKey),
                        new SqlParameter("@NoticeToValue",entity.NoticeToValue),
                        new SqlParameter("@IsActive",entity.IsActive),
                        new SqlParameter("@CreateTime",entity.CreateTime),
                        new SqlParameter("@CreateUserId",entity.CreateUserId),
                        new SqlParameter("@UpdateTime",entity.UpdateTime),
                        new SqlParameter("@UpdateUserId",entity.UpdateUserId),
                        new SqlParameter("@NoticeDesc",entity.NoticeDesc),
                        };


            var obj = SqlHelper.ExecuteScalar(CONNECTIONSTRINGS, CommandType.Text, strSql.ToString(), parameters);
            return obj == null ? 0 :
                Convert.ToInt32(obj);

        }

        public List<Entities.MonitorTaskInfo> GeTaskInfos()
        {
            var sql = $@"
                    SELECT  MT.MonitorId ,
                            MT.MonitorName ,
                            MT.MonitorSql ,
                            MT.AchieveCount ,
                            MT.DataServceId ,
                            MT.NoticeKey ,
                            MT.NoticeToValue ,
                            MT.NoticeDesc ,
                            MT.IsActive ,
                            MT.CreateTime ,
                            MT.CreateUserId ,
                            MT.UpdateTime ,
                            MT.UpdateUserId,
                            DS.DataConnect
                    FROM    dbo.MonitorTaskInfo AS MT WITH ( NOLOCK )
                            INNER JOIN dbo.DataServceConnect AS DS WITH ( NOLOCK ) ON DS.DataServceId = MT.DataServceId
                    WHERE   MT.IsActive = 1
                    ";
            var obj = SqlHelper.ExecuteDataset(CONNECTIONSTRINGS, CommandType.Text, sql);
            return DataTableToList<Entities.MonitorTaskInfo>(obj.Tables[0]);
        }
    }
}

